cd "\\micro.intra\Projekt\P1129$\P1129_Gem\Olle\Worker rep\"

use "\\micro.intra\Projekt\P1129$\P1129_Gem\Politikerdata\Data files\pol_ko.dta", clear
 joinby p_id valar using "\\micro.intra\Projekt\P1129$\P1129_Gem\Politikerdata\Data files\pol_ri.dta", unmatched(both)
  drop _merge
  destring llkk, force replace
joinby llkk parti_initial valar using num_elec_leader, unmatched(master)
 
 gen listleader=nrinom_hl==1 if vald==1 & nrinom!=. & num_leader==1 & num_elec>=5 & num_elec!=.
replace listleader=1 if nrinom==1 & vald==1 & nrinom_hl==. & valar<1990 & num_leader==1 & num_elec>=5 & num_elec!=.
 foreach var in nom nom_ri vald_ri vald listleader{
 	 bysort p_id: egen first_year_`var'=min(valar) if `var'==1
 }
 keep parti_initial nom first_year_* listleader vald_ri llkk p_id valar nom_ri  vald

 save pol_ko_popanalysis, replace

use sei_panel, clear
gen valar=1982 if year==1980
	replace valar=1985 if year==1985
		replace valar=1991 if year==1990
			replace valar=2002 if year==2002
				replace valar=2006 if year==2006
						replace valar=2010 if year==2010
							replace valar=2014 if year==2014
								replace valar=2018 if year==2018
keep if valar!=.
gen stud=sei==97 if sei!=.
keep if age>=18 & valar>1982 & worker_lim!=.

keep worker_lim worker_isco age p_id mun year valar stud  union_worker_prev union_prev  ssyk4_seiy union_worker_mainjob_prev
joinby p_id year using edu_panel, unmatched(master)
 drop _merge
joinby p_id using "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\draft_data.dta" , unmatched(master)
 drop _merge
 joinby p_id using "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\inc_res.dta" , unmatched(master)
 drop _merge
 joinby p_id using betyg_temp.dta , unmatched(master)
 drop _merge

joinby p_id using "\\micro.intra\Projekt\P1129$\P1129_Gem\Vanliga data\FodelseUppgifter.dta", unmatched(master) _merge(_merge)
drop _merge
destring Kon, force replace
gen woman=Kon==2
gen non_nordic=  VarldsdelNamn!="Sverige" &VarldsdelNamn!="Norden utom Sverige"
gen non_oecd= non_nordic==1 &  VarldsdelNamn!="EU28 utom Norden" & VarldsdelNamn!="Europa utom EU28 och Norden" &VarldsdelNamn!="Nordamerika"
gen age_b30 = age<30
gen age_30_39 = age>=30 & age<40
gen age_40_49 = age>=40 & age<50
gen age_50_64 = age>=50 & age<64
gen age_ab64 =  age>64

drop VarldsdelNamn VarldsdelMENA FodelseAr Kon


joinby p_id valar using pol_ko_popanalysis, unmatched(master)
	drop _merge	

	
	 foreach var in nom nom_ri vald_ri vald listleader{
	 	replace `var' =0 if `var'==.
		replace `var'=. if `var'==1 & first_year_`var'<valar

	 drop first_year_`var'
 }


destring mun llkk, force replace
replace mun =llkk if llkk!=.
egen m_y= group(mun valar)

bysort m_y: egen pop=count(p_id)
bysort y: egen pop_y=count(p_id)
gen pop_weight= pop_y/pop
drop pop pop_y

gen worker_lim_un=worker_lim*union_worker_prev
gen worker_lim_noun=worker_lim*(1-union_worker_prev)
gen nom_left= nom==1 & (parti_initial=="S" | parti_initial=="V") 
gen nom_notleft= nom==1 & parti_initial!="S" & parti_initial!="V"
foreach var in nom nom_left nom_notleft nom_ri vald_ri vald listleader{
	gen base = `var' if worker_lim==0
	egen  mean=mean(base)
gen `var'_rel=`var'/mean
drop mean base
}


joinby p_id valar using studforb, unmatched(master)
foreach var of varlist party- integration{
	replace `var'=0 if `var'==. & valar>=2010
	
}
foreach var of varlist   party_lag- integration_lag{
	replace `var'=0 if `var'==. & valar>=2014
	
}
drop _merge
joinby llkk parti_initial valar using num_elec_leader, unmatched(master)
drop _merge
replace stud=0 if stud==.
joinby ssyk4_seiy using  occ_stdtimeflex, unmatched (master)
drop _merge
joinby ssyk4_seiy using  onet_legtasks , unmatched (master)
drop _merge
joinby p_id valar using unionmemb_prev, unmatched (master)

gen worker_lim_unmemb=worker_lim*worker_union_latest 
gen worker_lim_nounmemb=worker_lim*(1-worker_union_latest )
